/*
 * @Author: linzaifei 1609781601@qq.com
 * @Date: 2025-08-15 14:27:40
 * @LastEditors: linzaifei 1609781601@qq.com
 * @LastEditTime: 2025-08-15 14:49:53
 * @FilePath: /vue-cesium-electron/src/utils/useCesiumMap.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import type { VcReadyObject } from '../../node_modules/vue-cesium/es/utils/types'
import * as Cesium from 'cesium'

export const useCesiumMap = () => {
    let viewer: Cesium.Viewer;
    let marker: Cesium.Entity;
    
    const initCesiumMap = (readyObj: VcReadyObject) => {
        // 添加类型断言
        viewer = readyObj.viewer as unknown as Cesium.Viewer;
        console.log('Cesium Viewer 已初始化:', viewer);
        // 你可以在这里添加更多的 Cesium 初始化代码
    }

    const getBounds = () => {
         if (!viewer) throw new Error('地图未初始化');
        // 获取当前视图的矩形范围
        const bounds = viewer.camera.computeViewRectangle()
        // 如果需要转换为经纬度
        if (bounds) {
            const gt_id2 = Cesium.Math.toDegrees(bounds.west)
            const lt_id2 = Cesium.Math.toDegrees(bounds.south)
            const gt_id1 = Cesium.Math.toDegrees(bounds.east)
            const lt_id1 = Cesium.Math.toDegrees(bounds.north)   
            return { gt_id2, lt_id2, gt_id1, lt_id1 }

        }
        return null;
    }

    const moveEnd=()=>{
        if (!viewer) throw new Error('地图未初始化');
        return viewer.camera.moveEnd;
    }

    const flyTo=(lng:number,lat:number)=>{
        console.log("________flyTo",lng,lat)
        if (!viewer) throw new Error('地图未初始化');
        viewer.camera.flyTo({
            destination:  Cesium.Cartesian3.fromDegrees(lng,lat,5000),
        })
    }

    const addMarker=(lng: number,lat: number, name:string)=>{
         if (!viewer) throw new Error('地图未初始化');
            if (marker) {
                viewer.entities.remove(marker);
            }
            marker = viewer.entities.add({
                position:Cesium.Cartesian3.fromDegrees(lng,lat,5000),
                billboard: {
                    image: "https://zouyaoji.top/vue-cesium/images/grepin.png", // 替换为你的图标
                    width: 40,
                    height: 40,
                    scale: 1.0,
                    scaleByDistance : new Cesium.NearFarScalar(1.5e2, 1.2, 8.0e6, 0.5),
                    heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                },
                label: {
                    text: name,
                    font: "10px sans-serif",
                    fillColor: Cesium.Color.WHITE,
                    outlineColor: Cesium.Color.BLACK,
                    outlineWidth: 2,
                    pixelOffset: new Cesium.Cartesian2(0.0, 25.0),
                    style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                    heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                },
            });   
     }




    return {
        initCesiumMap,
        getBounds,
        moveEnd,
        flyTo,
        addMarker,
    }
}